package com.zwface.demo.function.leave;

import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.zwface.demo.R;
import com.zwface.demo.base.BaseActivity;
import com.zwface.demo.comm.comm;
import com.zwface.demo.databinding.ActivityLeaveLsBinding;
import com.zwface.demo.function.leave.adapter.LeaveLsAdapter;
import com.zwface.demo.function.leave.mvvm.LeaveViewModel;
import com.zwface.demo.greendao.LeaveManager;
import com.zwface.demo.model.db.Leave;
import com.zwface.demo.utils.SharedPreferenceUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * created by      张中文
 * on:             2020/4/29 11:14
 * 描述：请假列表
 */
public class LeaveLsActivity extends BaseActivity<ActivityLeaveLsBinding>
{
    private LinearLayoutManager linearLayoutManager;
    private List<Leave> mData;
    private LeaveLsAdapter mAdapter;
    private LeaveViewModel viewModel;
    @Override
    public int onLayoutId() {
        return R.layout.activity_leave_ls;
    }

    @Override
    public void onInitViewDataBinding(ActivityLeaveLsBinding binding) {
        super.onInitViewDataBinding(binding);
        viewModel = ViewModelProviders.of(this).get(LeaveViewModel.class);
    }

    @Override
    public void onInitComponent() {
        super.onInitComponent();
        initTopbar();
        linearLayoutManager = new LinearLayoutManager(context) {
            @Override
            public RecyclerView.LayoutParams generateDefaultLayoutParams() {
                return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT);
            }
        };
        binding.recyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new LeaveLsAdapter();
        mAdapter.isTeacher(viewModel.isTeacher.get());
        mAdapter.setEmptyView(R.layout.layout_empty, (ViewGroup) binding.recyclerView.getParent());
        binding.recyclerView.setAdapter(mAdapter);
        getData();

        binding.recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                Leave leave = mData.get(position);
                switch (view.getId()){
                    case R.id.btn_pass:
                        leave.setStatus(comm.LEAVE_STATUS_PASS);
                        LeaveManager.updateLeave(leave);
                        mAdapter.notifyItemChanged(position);
                        break;
                    case R.id.btn_refuse:
                        leave.setStatus(comm.LEAVE_STATUS_REFUSE);
                        LeaveManager.updateLeave(leave);
                        mAdapter.notifyItemChanged(position);
                        break;
                }
            }
        });
    }

    private void initTopbar(){
        binding.topbar.addLeftBackImageButton().setOnClickListener(view -> finish());
        binding.topbar.setTitle("请假记录");
    }

    private void getData(){
        mData = new ArrayList<>();
        if(viewModel.isTeacher.get()){
            mData = LeaveManager.queryAllLeave();
        }else{
            String stuId = SharedPreferenceUtils.getParam(context,SharedPreferenceUtils.USER_ID);
            mData = LeaveManager.queryLeaveByStuId(stuId);
        }
        mAdapter.setNewData(mData);
    }
}
